package com.zhangzhan.gulimallproduct.app;

import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallproduct.entity.ProductAttrValueEntity;
import com.zhangzhan.gulimallproduct.service.AttrService;
import com.zhangzhan.gulimallproduct.vos.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-12 16:45:44
 */
@RestController
@RequestMapping("gulimallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallproduct:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询详细属性列表
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    //@RequiresPermissions("gulimallproduct:attr:list")
    public R attrTypeList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.attrTypeList(params, catelogId, attrType);

        return R.ok().put("page", page);
    }


    /**
     *
     */
    @RequestMapping("/base/listforspu/{spuId}")
    //@RequiresPermissions("gulimallproduct:attr:list")
    public R baseListforspu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntities = attrService.baseListforspu(spuId);

        return R.ok().put("data", productAttrValueEntities);
    }

    ///product/attr/update/{spuId}
    @RequestMapping(value = "/update/{spuId}",method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attr:list")
    public R attrValueUpdate(@PathVariable("spuId") Long spuId,
                             @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        attrService.attrValueUpdate(spuId, productAttrValueEntities);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping(value = "/info/{attrId}",method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {

        return R.ok().put("attr", attrService.getByIdCorrelationPath(attrId));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallproduct:attr:save")
    public R save(@RequestBody AttrVo attrVo) {
        attrService.saveDetailed(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:attr:update")
    public R update(@RequestBody AttrVo attrVo) {
        attrService.updateOrSaveDetailed(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallproduct:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
